{include file="common/head"/}
<style>
    .layui-btn {
        line-height: 30px;
    }
</style>
<div class="layui-fluid"><div class="layui-card"><div class="layui-card-body">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>部门管理{:lang('list')}</legend>
    </fieldset>
    <blockquote class="layui-elem-quote">
        <a href="{:url('add')}" class="layui-btn layui-btn-small" style="line-height: 38px;">{:lang('add')}部门管理</a>
    </blockquote>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
</div>
</div>


<script type="text/html" id="is_lock">
    {{# if(d.status==0){ }}
    <a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="is_lock">关闭</a>
    {{# }else{  }}
    <a class="layui-btn layui-btn-mini" lay-event="is_lock">开启</a>
    {{# } }}
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="action">
    <a href="{:url('edit')}?id={{d.id}}" class="layui-btn layui-btn-mini">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
</script>

{include file="common/foot"/}

<script>
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        var tableIn = table.render({
            id: 'user',
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            page: true,  //关闭分页展示
            cols: [[
                // {checkbox:true,fixed: true},
                {field: 'id', title: 'ID', width: 60},
                {field: 'username', title: '用户名', width: 120},
                {field: 'c_name', title: '分类', width: 120},
                {field: 'd_name', title: '部门', width: 180},
                {field: 'tel', title: '联系电话', width: 120},
                {field: 'ip', title: 'IP地址', width: 140, align: 'center'},
                // {field: 'sort', title:'排序', align:'center', width:120, templet: '#order'},
                {field: 'status', align: 'center',title: '{:lang("status")}', width: 120,toolbar: '#is_lock'},
                {title: '操作',     width: 180, align: 'center', toolbar: '#action'}
            ]],
            limit: 100 //每页默认显示的数量 ,此时可以关闭分页
        });
        //搜索
        $('#search').on('click', function() {
            var key = $('#key').val();
            if($.trim(key)==='') {
                layer.msg('{:lang("pleaseEnter")}关键字！',{icon:0});
                return;
            }
            tableIn.reload({
                where: {key: key}
            });
        });

        $('body').on('blur','.list_order',function() {
           var id = $(this).attr('data-id');
           var sort = $(this).val();
           $.post('{:url("ruleOrder")}',{id:id,sort:sort},function(res){
                if(res.code==1){
                    layer.msg(res.msg,{time:1000,icon:1},function(){
                        location.href = res.url;
                    });
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                }
           })
        })
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'is_lock') {
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post('{:url("status")}', {'id': data.id}, function (res) {
                    layer.close(loading);
                    console.log(res);
                    if (res.status === 1) {
                        if (res.is_lock === 0) {
                            obj.update({
                                is_lock: '<a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="is_lock">禁用</a>'
                            });
                        } else {
                            obj.update({
                                is_lock: '<a class="layui-btn layui-btn-mini layui-btn-warm" lay-event="is_lock">正常</a>'
                            });
                        }
                        tableIn.reload();
                    } else {
                        layer.msg('操作失败！', {time: 1000, icon: 2});
                        return false;
                    }
                })
            }else if (obj.event === 'del') {
                layer.confirm('您确定要删除该吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('del')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });

        $('#delAll').click(function(){
            layer.confirm('确认要删除选中信息吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('user'); //test即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if (ids == '')
                {
                    layer.msg('请勾选待删除的数据!', {time: 1000, icon: 2});
                    return;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delAll')}", {ids: ids}, function (data) {
                    console.log(data);
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })
    });
</script>
</body>
</html>